<?php
/**
 * 删除操作
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types=1);

namespace app\admin\traits;

use support\Response;
use think\db\Query;

trait AdminDelTrait
{
    /**
     * 删除
     * @return Response
     */
    public function del(): Response
    {
        $request = request();
        $model   = $this->model;

        $id = ($request)->input('id');

        $check = $model->inNoDeletionIds($id);
        if (false !== $check) {
            return admin_error('ID为' . $check . '的数据不能被删除');
        }

        $result = $model::destroy(static function ($query) use ($id) {
            /** @var Query $query */
            $query->whereIn('id', $id);
        });

        return $result ? admin_success('删除成功', [], URL_RELOAD) : admin_error('删除失败');
    }
}
